﻿using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Classification;
using Microsoft.VisualStudio.Utilities;
using BasicallyNot.CodeTransformers;
using BasicallyNot.CodeTransformers.Searchers;

namespace BasicallyNot.Classifiers
{

	internal class MemberTypeDeducer
	{

		internal static LinkedList<Keyword> Keywords = new LinkedList<Keyword>();

		static MemberTypeDeducer()
		{
			IVisualSearcher containsValue = new ContainsValue();

			Keywords.AddLast( new Keyword( "Namespace",		containsValue ) );
			Keywords.AddLast( new Keyword( "Module",			containsValue ) );
			Keywords.AddLast( new Keyword( "Structure",		containsValue ) );
			Keywords.AddLast( new Keyword( "Class",				containsValue ) );
			Keywords.AddLast( new Keyword( "Interface",		containsValue ) );
			Keywords.AddLast( new Keyword( "Enum",				containsValue ) );
			Keywords.AddLast( new Keyword( "Sub",					containsValue ) );
			Keywords.AddLast( new Keyword( "Function",		containsValue ) );
			Keywords.AddLast( new Keyword( "Property",		containsValue ) );
			Keywords.AddLast( new Keyword( "Get",					containsValue ) );
			Keywords.AddLast( new Keyword( "Set",					containsValue ) );
		}



		internal static IList<ClassificationSpan> GetClassificationSpans( SnapshotSpan span, IClassificationType classificationType )
		{
			//create a list to hold the results
			List<ClassificationSpan> classifications = new List<ClassificationSpan>();
			string spanText = span.GetText().ToLower();

			VisualSearcherEventArg searchArg = new VisualSearcherEventArg( spanText );
			VisualSearchResult searchResult = new VisualSearchResult();
			IVisualSearcher search;

			foreach ( Keyword keyword in MemberTypeDeducer.Keywords ) {
				search = keyword.Searcher;
				searchArg.Keyword = keyword.Lower;
				search.Match( searchArg, ref searchResult );

				if ( searchResult.IsMatch ) {
					classifications.Add( new ClassificationSpan( new SnapshotSpan( span.Snapshot, new Span( span.Start + searchResult.MatchIndex, keyword.Value.Length ) ), classificationType ) );
				}
			}
			
			return classifications;
		}

	}

}
